/**
*    Copyright (C) 2008  Chase Kernan 
*    chase.kernan@gmail.com
*
*    This program is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.
* 
**/

package com.chasekernan.hxnova.server;

import haxe.io.Bytes;

#if neko
import neko.FileSystem;
import neko.io.File;
import neko.io.FileInput;
import neko.io.FileOutput;
import neko.zip.Compress;
import neko.zip.Uncompress;
import neko.db.Connection;
import neko.db.ResultSet;
import neko.db.Sqlite;
import haxe.io.Bytes;

#elseif php
import php.FileSystem;
import php.io.File;
import php.io.FileInput;
import php.io.FileOutput;
#end

/**
    Contains all of the IO operations necessary for the neko backend.
**/
class IO {
    public static var COMPRESSION_LEVEL : Int = 6;
    
    /**
        Compresses a file using ZLib.
    **/
    public static function compress(stuff : String) : String {
        #if neko
            #if compress
            var bytes = Compress.run(Bytes.ofString(stuff), COMPRESSION_LEVEL);
            return bytes.readString(0, bytes.length);
            #else
            return stuff;
            #end
            
        #else
        return stuff;
        #end
    }
    
    public static function uncompress(stuff : String) : String {
        #if neko
            #if compress
            var bytes = Uncompress.run(Bytes.ofString(stuff));
            return bytes.readString(0, bytes.length);
            #else
            return stuff;
            #end
            
        #else
        return stuff;
        #end
    }
    
    public static function fileExists(file : String) : Bool {
        #if php
        return FileSystem.exists(file);
        #elseif neko
        return FileSystem.exists(file);
        #else
        return false;
        #end
    }
    
    public static function makeFolder(folderName : String) {
        #if neko
        if (FileSystem.exists(folderName)) return;
        FileSystem.createDirectory(/*FileSystem.fullPath(*/folderName/*)*/);
        #elseif php
        if (FileSystem.exists(folderName)) return;
        FileSystem.createDirectory(/*FileSystem.fullPath(*/folderName/*)*/);
        #end
    }
    
    #if neko
    public static function writeToFile(stuff : String, path : String) {
        var out : FileOutput = null;
        try {
            #if compress
                out = File.write(path, true);
            #else
                out = File.write(path, false);
            #end
            out.writeString(stuff);
            out.close();
        } catch (e : Dynamic) {
            if(out != null) out.close();
            throw "Error while trying to write to file, " + path + ": " + e;
        }
    }
    
    #elseif php
    public static function writeToFile(stuff : String, path : String) {
        var out : FileOutput = null;
        try {
            out = File.write(path, false);
            out.writeString(stuff);
            out.close();
        } catch (e : Dynamic) {
            if(out != null) out.close();
            throw "Error while trying to write to file, " + path + ": " + e;
        }
    }
    
    #else
    public static function writeToFile(stuff : String, path : String) {}
    #end
    
    #if neko
    public static function readFromFile(path : String) : String {
        var inFile : FileInput = null;
        try {
            path = FileSystem.fullPath(path);
            
            #if compress
                inFile = File.read(path, true);
            #else
                inFile = File.read(path, false);
            #end
            
            var bytes = inFile.readAll();
            var stuff = bytes.readString(0, bytes.length);
            inFile.close();
            return stuff;
        } catch (e : Dynamic) {
            if(inFile != null) inFile.close();
            throw "Error while trying to read from file, " + path + ": " + e;
        }
    }
    
    #elseif php
    public static function readFromFile(path : String) : String {
        var inFile : FileInput = null;
        try {
            path = FileSystem.fullPath(path);
            
            inFile = File.read(path, false);
            
            var bytes = inFile.readAll();
            var stuff = bytes.readString(0, bytes.length);
            inFile.close();
            return stuff;
        } catch (e : Dynamic) {
            if(inFile != null) inFile.close();
            throw "Error while trying to read from file, " + path + ": " + e;
        }
    }
    
    #else
    public static function readFromFile(path : String) : String {return null;}
    #end
    
//    public static function executeSQL(db : String, command : String) : ResultSet {
//        var cnx : Connection;
//        try {
//            cnx = Sqlite.open(db);
//            var results : ResultSet = cnx.request(command);
//            cnx.close();
//            return results;
//        } catch (e : Dynamic) {
//            if (cnx != null) cnx.close();
//            throw "Error while trying to request from database, " + db + " with command, " + command + ": " + e;
//        }
//    }
    
}